#pragma once

#include <windows.h>
#include <mmsystem.h>
#include <stdio.h>
#include "vfw.h"
#include <tchar.h>
#include <iostream>
#pragma comment (lib,"winmm.lib")
#include <conio.h> 
using namespace std;
class CMp3Player
{
protected:
	DWORD m_idPlayer;
	CMp3Player(DWORD id)
	{
		m_idPlayer = id;
	}
public:
	//Open_File
	//Thong tin:
	//   - Khoi tao doi tuong CMp3Player
	//   - Tham so dua vao la duong dan file MP3
	static CMp3Player* Open_File(CHAR *lpFileName)
	{
		#pragma warning (disable:4311)

		// Tham so cho Media
		MCI_OPEN_PARMS mciOpenParms;

		// Driver de play MP3 (tham khao them trong MSDN)
		mciOpenParms.lpstrDeviceType = "mpegvideo";
		// File mp3 se play
		mciOpenParms.lpstrElementName = lpFileName;

		// Khoi tao CMp3Player
		DWORD dwResult = mciSendCommand(0, MCI_OPEN,MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,(DWORD) &mciOpenParms);    
		// Loi khoi tao (co the do soundCard)
		if (dwResult != 0 )
			return NULL;

		// Thiet lap tham so cho CMp3Player (su dung don vi Milisecond)  
		MCI_SET_PARMS param;
		param.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
		dwResult = mciSendCommand(mciOpenParms.wDeviceID,MCI_SET,MCI_SET_TIME_FORMAT,(DWORD) (LPMCI_SET_PARMS) &param);
		// Loi thiet lap tham so
		if (dwResult != 0 )
			return NULL;

		#pragma warning (default:4311)

		// Khoi tao doi tuong Mp3
		return new CMp3Player( mciOpenParms.wDeviceID );    
	}

	virtual ~CMp3Player()
	{
		// Huy CMp3Player
		mciSendCommand(m_idPlayer, MCI_CLOSE, 0, NULL);
	}

	void Release(){delete this;}

	// Play()
	// Thong tin: Play file nhac
	void Play()
	{
		#pragma warning (disable:4311)
		MCI_PLAY_PARMS param;
		param.dwCallback = (DWORD) NULL;
		mciSendCommand(m_idPlayer, MCI_PLAY, MCI_NOTIFY,(DWORD)(LPVOID) &param);
		#pragma warning (default:4311)
	}

	// Stop()
	// Thong tin: Dung file nhac   
	void Stop()
	{
		#pragma warning (disable:4311)
		MCI_GENERIC_PARMS param;
		param.dwCallback = NULL;
		mciSendCommand(m_idPlayer,MCI_STOP,MCI_NOTIFY,(DWORD) (LPMCI_GENERIC_PARMS) &param);
		#pragma warning (default:4311)
	}
	
	// Pause()
	// Thong tin: Tam dung file nhac   
	void Pause()
	{
		#pragma warning (disable:4311)
		MCI_GENERIC_PARMS param;
		param.dwCallback = NULL;
		mciSendCommand(m_idPlayer,MCI_PAUSE,MCI_NOTIFY,(DWORD)(LPMCI_GENERIC_PARMS) &param);
		#pragma warning (default:4311)
	}
	// Resume()
	// Thong tin: Tiep tuc play (do pause)
	void Resume()
	{
		#pragma warning (disable:4311)
		MCI_GENERIC_PARMS param;
		param.dwCallback = NULL;
		mciSendCommand(m_idPlayer,MCI_RESUME,MCI_NOTIFY,(DWORD)(LPMCI_GENERIC_PARMS) &param);
		#pragma warning (default:4311)
	}
	// Length()
	// Thong tin: Tra ve thoi gian file mp3 (milisecond)
	DWORD Get_Length()
	{
		#pragma warning (disable:4311)
		MCI_STATUS_PARMS param;
		param.dwItem = MCI_STATUS_LENGTH;
		DWORD dwResult = mciSendCommand(m_idPlayer,MCI_STATUS ,MCI_STATUS_ITEM, (DWORD) (LPMCI_STATUS_PARMS) &param);
		#pragma warning (default:4311)
		if (dwResult != 0)
			return false;

		return (DWORD)param.dwReturn;
	}

	// Get_Position()
	// Thong tin: Tra ve vi tri (thoi diem milisecond) dang play
	DWORD Get_Position()
	{
		#pragma warning (disable:4311)
		MCI_STATUS_PARMS param;
		param.dwItem = MCI_STATUS_POSITION;;
		DWORD dwResult = mciSendCommand(m_idPlayer,MCI_STATUS ,MCI_STATUS_ITEM, (DWORD) (LPMCI_STATUS_PARMS) &param);
		#pragma warning (default:4311)
		if (dwResult != 0)
			return false;
		return (DWORD)param.dwReturn;
	}

	// Seek()
	// Thong tin: Nhay den vi tri seekto de play (milisecond)
	void Seek(DWORD seekto)
	{
		#pragma warning (disable:4311)
		MCI_SEEK_PARMS param;
		param.dwTo = seekto;   
		mciSendCommand(m_idPlayer,MCI_SEEK,MCI_TO,(DWORD) (LPMCI_SEEK_PARMS) &param);	
		#pragma warning (default:4311)
	}
	void restart() {
		Seek(0);
		Play();
	}
	void loop() {
		if (Get_Position() >= Get_Length()) {
			restart();
		}
	}
	bool End() {
		return Get_Position() >= Get_Length();
	}
};

